<?php

namespace App\Http\Controllers;

use App\Project;
use App\AwardApplicationFormData;
use App\AwardGroup;
use App\ExpertGroup;
use App\Doc;
use App\Services\AwardService;
use App\Services\LogService;
use App\Services\CommonService;
use App\User;
use Auth, Request;
use App\Services\AuthService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Session;

class CommitteeWebController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', [
            'except' => [
                'index',
                'login',
            ],
        ]);
        $this->authSvc = new AuthService;
        $this->awardSvc = new AwardService;
        $this->logSvc = new LogService;
        $this->commonSvc = new CommonService;
    }

    public function login() {
       return view('committee.login');
    }

    public function index()
    {
        /** @var User $user */
        $user = Request::user();
        if ($user && $user->isCommittee()) {
            if ($user->isLargeGroupLeader()) {
                $redirectTo = '/committee/manage-large-group-expert-review';
            } else {
                $redirectTo = '/committee/vote?large_group_id=1';
            }
            return redirect($redirectTo);
        } else {
            $redirectTo = '/committee/login';
            return redirect($redirectTo);
        }
    }

    /*
     * 大组管理-网评
     */
    public function manageLargeGroupExpertReview()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-expert-review', ['user' => $user]);
    }

    /*
     * 大组管理-划定待定区
     */
    public function manageLargeGroupMeetingReview()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        $largeGroupId = $user->leading_large_group_id;
        $awardId = DB::table('large_groups')->where('id', $largeGroupId)->value('award_id');
        Log::debug("【manageLargeGroupMeetingReview】awardId=$awardId");
        return view('committee.manage-large-group-meeting-review', ['user' => $user, 'awardId' => $awardId]);
    }

    /*
     * 大组管理-投票
     */
    public function manageLargeGroupVote()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-vote', ['user' => $user]);
    }

    public function manageLargeGroupDefense() {
        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-defense', ['user' => $user]);
    }

    /*
     * 大组管理-待定区投票
     */
    public function manageLargeGroup3()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-3', ['user' => $user]);
    }

    /*
     * 大组管理-会评
     */
    public function manageLargeGroup5()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-5', ['user' => $user]);
    }

    /*
     * 大组管理-授奖
     */
    public function manageLargeGroupAward()
    {
//return view('404');
        /** @var User $user */
        $user = Request::user();
        if (!$user->isLargeGroupLeader()) die('Not large group leader');
        return view('committee.manage-large-group-award', ['user' => $user]);
    }

    /**
     * 委员待定区投票
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vote1()
    {

        /** @var User $user */
        $user = Request::user();
        if (!$user->isCommittee()) die('Not committee');
        return view('committee.vote-1', ['user' => $user]);
    }

    /**
     * 委员终轮投票
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vote()
    {
        /** @var User $user */
        $user = Request::user();
        if (!$user->isCommittee()) die('Not committee');
        return view('committee.vote', ['user' => $user]);
    }

    public function review(){
        /** @var User $user */
        $user = Request::user();
        if (!$user->isCommittee()) die('Not committee');
        return view('committee.review', ['user' => $user]);
    }
}
